package com.tjb.reggie.util;

import com.tjb.reggie.common.TjbException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 1
 */
@Component
public class MailUtil {

    /**
     * 邮件发送者
     */
    @Value("${spring.mail.username}")
    private String MAIL_SENDER;

    /**
     *注入QQ发送邮件的bean
     */
    @Resource
    private JavaMailSender javaMailSender;


    /**
     * 发送文本邮件
     *
     * @param qqNumber
     * @param template
     * @return
     */
    @Async
    public void sendSimpleMail(String qqNumber, String template) {
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //发送者
            mailMessage.setFrom(MAIL_SENDER);
            //接收者
            mailMessage.setTo(qqNumber);
            //邮件标题
            mailMessage.setSubject("【瑞吉外卖】");
            //邮件内容
            mailMessage.setText(template);
            //发送邮箱
            javaMailSender.send(mailMessage);
        } catch (Exception e) {
            throw new TjbException(e.getMessage());
        }
    }
}
